AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

ENT.Planet = nil
ENT.RefireRate = 20

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_NONE )
	self.Entity:SetMoveType( MOVETYPE_NONE )
	self.Entity:SetSolid( SOLID_NONE )
	self.Entity:SetNotSolid(true)
	self.Entity:DrawShadow(false)
	self.Entity:SetColor(0,0,0,0)
end

function ENT:Think()
	local Atmosphere = self.Planet
	local MaxMinerals = GetConVarNumber("mining_maxMinerals")
	self.RefireRate =  GetConVarNumber("mining_MineralSpawnDelay")
	
	if Atmosphere != nil then
		local SphereEnts = ents.FindInSphere(Atmosphere:GetPos(),Atmosphere.sbenvironment.size)
		local PlanetDeposits = {}
		local Pos = Atmosphere:GetPos() + Vector(0,0,Atmosphere.sbenvironment.size)
			
		for c,s in pairs(SphereEnts) do
			if s:GetClass() == "mining_mineral_deposit" then
				table.insert(PlanetDeposits,s)
			end
		end
	
		if #PlanetDeposits > MaxMinerals then	
			PlanetDeposits[1]:Remove()
			table.remove(PlanetDeposits, 1)
		end
		
		if Atmosphere.sbenvironment.temperature > 308 then
			if #PlanetDeposits < MaxMinerals then
				local Ang = Angle(math.random(40,90),math.random(0,360),0)

				local tracedata = {}
				tracedata.start = Pos
				tracedata.endpos = Pos + ((Atmosphere.sbenvironment.size * 1.5) * Ang:Forward())
				tracedata.mask = MASK_WATER | CONTENTS_SOLID
				local Trace = util.TraceLine(tracedata)
				//Check if it hit something.
				if Trace.Hit then
					//Not sure if HitWorld will return false if it hits nothing, so just in case, we have two checks.
					if string.find(Trace.HitTexture,"displacement") != nil then
						Ang = Trace.HitNormal:Angle() + Angle(90,0,0)
						local Mineral = ents.Create("mining_mineral_deposit")
					
						Mineral:SetModel("models/Tiberium/tiberium_parent.mdl")
						Mineral:SetSkin(2)
						Mineral:SetAngles(Ang)
						Mineral:SetPos(Trace.HitPos - (Trace.HitNormal * 3))
					
						//Mineral.Ore = math.ceil(math.random(3000,6000))
						Mineral.Polonium = math.ceil(math.random(2500,4000))
						Mineral.CrysType = 2
					
						Mineral:Spawn()
					end
				end
			end
		end
			
		if Atmosphere.sbenvironment.temperature < 283 then
			if #PlanetDeposits < MaxMinerals then
				local Ang = Angle(math.random(40,90),math.random(0,360),0)
			
				local tracedata = {}
				tracedata.start = Pos
				tracedata.endpos = Pos + ((Atmosphere.sbenvironment.size * 1.5) * Ang:Forward())
				tracedata.mask = MASK_WATER | CONTENTS_SOLID
				local Trace = util.TraceLine(tracedata)
				//Check if it hit something.
				if Trace.Hit then
					//Not sure if HitWorld will return false if it hits nothing, so just in case, we have two checks.
					if string.find(Trace.HitTexture,"displacement") != nil then
						Ang = Trace.HitNormal:Angle() + Angle(90,0,0)
						local Mineral = ents.Create("mining_mineral_deposit")
					
						Mineral:SetModel("models/Tiberium/tiberium_parent.mdl")
						Mineral:SetSkin(1)
						Mineral:SetAngles(Ang)
						Mineral:SetPos(Trace.HitPos - (Trace.HitNormal * 3))
					
						//Mineral.Ore = math.ceil(math.random(300,1000))
						Mineral.Polonium = math.ceil(math.random(3500,5000))
						Mineral.CrysType = 1
					
						Mineral:Spawn()
					end
				end
			end
		end
	
		if Atmosphere.sbenvironment.temperature > 283 and Atmosphere.sbenvironment.temperature < 308 and Atmosphere.sbenvironment.air.o2per < 5 then
			if #PlanetDeposits < MaxMinerals then
				local Ang = Angle(math.random(40,90),math.random(0,360),0)
			
				local tracedata = {}
				tracedata.start = Pos
				tracedata.endpos = Pos + ((Atmosphere.sbenvironment.size * 1.5) * Ang:Forward())
				tracedata.mask = MASK_WATER | CONTENTS_SOLID
				local Trace = util.TraceLine(tracedata)
				//Check if it hit something.
				if Trace.Hit then
					//Not sure if HitWorld will return false if it hits nothing, so just in case, we have two checks.
					if string.find(Trace.HitTexture,"displacement") != nil then
						Ang = Trace.HitNormal:Angle() + Angle(90,0,0)
						local Mineral = ents.Create("mining_mineral_deposit")
					
						Mineral:SetModel("models/Tiberium/tiberium_parent.mdl")
						Mineral:SetSkin(0)
						Mineral:SetAngles(Ang)
						Mineral:SetPos(Trace.HitPos - (Trace.HitNormal * 3))
					
						//Mineral.Ore = math.ceil(math.random(300,1000))
						Mineral.Polonium = math.ceil(math.random(4500,6000))
						Mineral.CrysType = 3
					
						Mineral:Spawn()
					end
				end
			end
		end
	
		if PlanetDeposits[1] != nil and Atmosphere.sbenvironment.temperature < 308 and Atmosphere.sbenvironment.temperature > 283 and Atmosphere.sbenvironment.air.o2per > 5 then
			PlanetDeposits[1]:Remove()
			table.remove(PlanetDeposits, 1)
		end
	end
	
	self.Entity:NextThink(CurTime() + self.RefireRate)
	return true
end